package cn.stylefeng.guns.modular.rest.hapi;


import cn.stylefeng.guns.modular.basic.entity.BasicCustomer;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.ha.entity.HaApiDeliToStore;
import cn.stylefeng.guns.modular.ha.entity.HaApiDepartOrder;
import cn.stylefeng.guns.modular.ha.model.params.HaApiDeliToStoreParam;
import cn.stylefeng.guns.modular.ha.service.HaAgvTaskService;
import cn.stylefeng.guns.modular.ha.service.HaApiDeliToStoreService;
import cn.stylefeng.guns.modular.ha.service.HaApiDepartOrderService;
import cn.stylefeng.guns.modular.rest.xlbapi.vo.DeliToStoreIby;
import cn.stylefeng.guns.modular.rest.xlbapi.vo.SyncDepartOrderIby;
import cn.stylefeng.guns.modular.rest.xlbapi.vo.SyncDepartOrderListIby;
import cn.stylefeng.guns.modular.transport.entity.TDispatch;
import cn.stylefeng.guns.modular.transport.entity.TransportReceipt;
import cn.stylefeng.guns.modular.transport.service.TDispatchService;
import cn.stylefeng.guns.modular.transport.service.TransportReceiptService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Date;

@Api(tags = "恒安接口调用")
@Controller
@RequestMapping("/rest/haApi")
public class RestHaApiController {


    @Autowired
    private HaApiDeliToStoreService haApiDeliToStoreService;

    @Autowired
    private HaApiDepartOrderService haApiDepartOrderService;

    @Autowired
    private BasicCustomerService basicCustomerService;

    @Autowired
    private TransportReceiptService transportReceiptService;

    @Autowired
    private TDispatchService tDispatchService;

    // 同步发车单信息
    @ApiOperation(value = "同步发车单信息", notes = "恒安装车完成，发运的时候将发运成功的发车单信息通过该接口同步给极鹿系统")
    @ResponseBody
    @RequestMapping(value = "/syncDepartOrder", method = {RequestMethod.POST})
    public ResponseData syncDepartOrder(@RequestBody SyncDepartOrderIby syncDepartOrderIby) {
        if(ToolUtil.isEmpty(syncDepartOrderIby.getDepartOrderId())){
            return ResponseData.error("发车单号不能为空");
        }
        //判断是否重复调用
        Integer count = haApiDepartOrderService.lambdaQuery()
                .eq(HaApiDepartOrder::getDepartOrderId,syncDepartOrderIby.getDepartOrderId())
                .count();
        if(count> 0){
            return ResponseData.success("["+syncDepartOrderIby.getDepartOrderId() + "]重复发车！");
        }

        BasicCustomer basicCustomer = null;
        if(ToolUtil.isEmpty(syncDepartOrderIby.getDeliStoreId())){
            return ResponseData.error("发货门店不能为空！");
        }else{
            basicCustomer = basicCustomerService.findListByStoreIdXlb(syncDepartOrderIby.getDeliStoreId());
            if(ToolUtil.isEmpty(basicCustomer)){
                return ResponseData.error("发货门店ID:"+syncDepartOrderIby.getDeliStoreId()+" 极鹿未配置，无法接收数据！");
            }
        }

        return haApiDepartOrderService.syncDepartOrder(syncDepartOrderIby, basicCustomer);
//        return ResponseData.success();
    }

    // 同步发车单信息
    @ApiOperation(value = "同步发车单信息", notes = "恒安装车完成，发运的时候将发运成功的发车单信息通过该接口同步给极鹿系统")
    @ResponseBody
    @RequestMapping(value = "/syncDepartOrderList", method = {RequestMethod.POST})
    public ResponseData syncDepartOrderList(SyncDepartOrderListIby syncDepartOrderListIby) {
        return ResponseData.success();
    }

    // 同步门店送达信息
    @Transactional(rollbackFor = Exception.class,timeout = 1800)
    @ApiOperation(value = "同步门店送达信息", notes = "司机送达完成后，调用该接口同步发车单送达状态，根据门店完成的送达数量自动判断发车单是否全部完成")
    @ResponseBody
    @RequestMapping(value = "/deliToStore", method = {RequestMethod.POST})
    public ResponseData deliToStore(@RequestBody DeliToStoreIby deliToStoreIby) {
        if(ToolUtil.isEmpty(deliToStoreIby.getDepartOrderId())){
            return ResponseData.error("发车单号不能为空");
        }
        //判断是否重复调用
        Integer count = haApiDeliToStoreService.lambdaQuery()
                .eq(HaApiDeliToStore::getDepartOrderId,deliToStoreIby.getDepartOrderId())
                .count();
        if(count> 0){
            return ResponseData.success("["+deliToStoreIby.getDepartOrderId() + "]重复送达！");
        }
        Integer count1 = tDispatchService.lambdaQuery()
                .eq(TDispatch::getDispatchNumber, deliToStoreIby.getDepartOrderId())
                .count();
        if(count1 < 1){
            return ResponseData.success("发车单号不存在");
        }
        // 批量修改送达状态
        transportReceiptService.lambdaUpdate()
                .eq(TransportReceipt::getDispatchNumber,deliToStoreIby.getDepartOrderId())
                .set(TransportReceipt::getSdtstate,"已送达")
                .set(TransportReceipt::getSdtime,deliToStoreIby.getDeliverTm())
                .set(TransportReceipt::getSdusername,deliToStoreIby.getDeliverName())
                .update();

        // 批量修改送达状态
        tDispatchService.lambdaUpdate()
                .eq(TDispatch::getDispatchNumber,deliToStoreIby.getDepartOrderId())
                .ne(TDispatch::getTstate,"待定价")
                .set(TDispatch::getTstate,"已完成")
                .set(TDispatch::getReturnTstate,"已回单")
                .update();

        //添加调用记录
        HaApiDeliToStoreParam addObj = new HaApiDeliToStoreParam();
        ToolUtil.copyProperties(deliToStoreIby, addObj);
        addObj.setApiTm(new Date());
        haApiDeliToStoreService.add(addObj);

        return ResponseData.success();
    }
}
